/**
 * 给图片添加自定义属性src
 * 用法 <img v-real-img="真是请求图片地址" src="默认图片地址" />
 */
let imageIsExist = function(url) {
    return new Promise((resolve) => {
        var img = new Image();
        img.onload = function() {
            if (this.complete == true) {
                return resolve(url)
            }
        }
        img.onerror = function() {
            return resolve(false)
        }
        img.src = url;
    })
}
const VReaimg = {
    inserted: async function(el, binding) {  //指令名称v-reaimg
        let imgURL = binding.value; //获取图片地址
        if (imgURL) {
            if (imgURL.charAt(imgURL.length - 1) == '/') {
                return
            }
            if (imgURL && el.getAttribute('src') !== imgURL) {
                let res = await imageIsExist(imgURL, el)
                if (res) {
                    el.setAttribute('src', imgURL)
                }
            }
        }
    }
}
export default VReaimg;